<html>
<head><meta charset="utf-8"><title>Exercising miri validation · miri · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/index.html">miri</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html">Exercising miri validation</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222435823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222435823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> P Queen <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222435823">(Jan 12 2021 at 14:15)</a>:</h4>
<p>Hi!</p>
<p>Are there any documentation or good sources of examples anywhere on how to try and exercise certain forms of miri validation through test cases? I'm trying to work this out on a known bug and coming up with limited results.</p>



<a name="222455125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222455125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tm <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222455125">(Jan 12 2021 at 16:21)</a>:</h4>
<p>Miri test cases should showcase issues it can detect <a href="https://github.com/rust-lang/miri/tree/master/tests/compile-fail">https://github.com/rust-lang/miri/tree/master/tests/compile-fail</a>, although I am not sure if this is what you are looking for?</p>



<a name="222722393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222722393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> P Queen <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222722393">(Jan 14 2021 at 14:19)</a>:</h4>
<p>This is perfect as an instructive set of examples, thanks.</p>



<a name="222862547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222862547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222862547">(Jan 15 2021 at 14:19)</a>:</h4>
<p>that's a good question... writing the right testcases can be tricky since for some issues Miri works best with specifically crafted testcases</p>



<a name="222862641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222862641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222862641">(Jan 15 2021 at 14:19)</a>:</h4>
<p>I know of one <em>good</em> example for this, which is how to test an iterator that yields mutable references:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// Gather all references from a mutable iterator and make sure Miri notices if</span>
<span class="c1">// using them is dangerous.</span>
<span class="k">fn</span> <span class="nf">test_all_refs</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="o">'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">dummy</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">T</span><span class="p">,</span><span class="w"> </span><span class="n">iter</span>: <span class="nc">impl</span><span class="w"> </span><span class="nb">Iterator</span><span class="o">&lt;</span><span class="n">Item</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Gather all those references.</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">refs</span>: <span class="nb">Vec</span><span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">iter</span><span class="p">.</span><span class="n">collect</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="c1">// Use them all. Twice, to be sure we got all interleavings.</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">refs</span><span class="p">.</span><span class="n">iter_mut</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">swap</span><span class="p">(</span><span class="n">dummy</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">refs</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">swap</span><span class="p">(</span><span class="n">dummy</span><span class="p">,</span><span class="w"> </span><span class="n">r</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="222862713"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222862713" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222862713">(Jan 15 2021 at 14:20)</a>:</h4>
<p>this code found issues in 2 or 3 iterators in the standard library</p>



<a name="222862834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/269128-miri/topic/Exercising%20miri%20validation/near/222862834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/269128-miri/topic/Exercising.20miri.20validation.html#222862834">(Jan 15 2021 at 14:21)</a>:</h4>
<p>this could also provide some more general guidance, such as -- if the interface lets you grab a bunch of references that are all live at once, then make sure to actually <em>use</em> all of them so that Miri truly checks if theses accesses are okay</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>